<?php

namespace app\admin\controller;

use app\admin\model\Page;
use app\common\model\Slideshow as ModelSlideshow;
use app\admin\model\Notice as ModelNotice;

/**
 * 通知管理的所有操作
 *
 * 时间 ： 2020年4月23日13:06:58
 * 作者 ： 夏立军
 */
class Slideshow extends AdminBase
{

    // 首页
    public function index()
    {
        // 加载轮播图的工具类
        $slideshow = new ModelSlideshow();

        // 数据库中的全部数量
        $slideshow_count = $slideshow->get_slideshow_count();

        // 获取轮播图的最大数量 
        $slideshow_max_size = config('ams_config.slideshow_max_size') ?? 3;

        // 是否可以继续添加一条新的轮播图
        $is_add_slideshow = ($slideshow_count < $slideshow_max_size);

        // 获取所有的轮播图数据
        $slideshow_info = $slideshow->get_slideshow_info();

        $notice = new ModelNotice();
        $slideshow_info_data = [];
        foreach ($slideshow_info as $row) {
            if ($row['nid'])
                $row['notice_name'] = $notice->get_notice_data_by_id($row['nid'])['title'];
            else
                $row['notice_name'] = "无";
            $slideshow_info_data[] = $row;
        }


        return view('index', [
            'slideshow_max_size' => $slideshow_max_size,
            'is_add_slideshow' => $is_add_slideshow,
            'slideshow_info' => $slideshow_info_data,
            'empty' => '<tr><td colspan="8" style="text-align:center">当前没有轮播图的数据，请及时添加</td></tr>'
        ]);
    }

    // 添加轮播图页面
    public function add()
    {
        $notice = new ModelNotice();
        $notice_data = $notice->get_display_notice_info();
        return view('add', [
            'notice_data' => $notice_data,
        ]);
    }

    // 添加轮播图操作
    public function add_slideshow()
    {
        $image_files = request()->file('image_file');
        $img_path = '';
        if (!$image_files) {
            return json(['code' => 1, 'msg' => '未接收到图片']);
        } else {
            $img_path = \think\facade\Filesystem::disk('public')->putFile('slideshow', $image_files);
        }

        $title = input('post.title/s', null);
        $nid = input('post.notice_id/d', null);
        $status = input('post.status/s', 1);

        // 加载轮播图的工具类
        $slideshow = new ModelSlideshow();

        $slideshow_res = $slideshow->add_slideshow_info($title, $img_path, $nid, $status);
        if (!$slideshow_res) {
            try {
                unlink(app()->getRootPath() . 'public/storage/' . $img_path);
            } catch (\Throwable $th) {
            }
            return json(['code' => 2, 'msg' => '新增失败，请检查文章链接，以及文件大小 20M']);
        }

        return json(['code' => 0, 'msg' => '新增成功']);
    }

    // 编辑轮播图页面
    public function edit()
    {
        $id = input('get.id/d', null);
        if (is_null($id)) {
            return redirect_index('id 丢失', url('Role/index'));
        }

        $notice = new ModelNotice();
        $notice_data = $notice->get_display_notice_info();

        $slideshow = new ModelSlideshow();
        $slideshow_info = $slideshow->get_slideshow_info_by_id($id);



        if ($slideshow_info['nid'])
            $slideshow_info['notice_name'] = $notice->get_notice_data_by_id($slideshow_info['nid'])['title'];
        else
            $slideshow_info['notice_name'] = "";


        return view('edit', [
            'notice_data' => $notice_data,
            'slideshow_info' => $slideshow_info,
        ]);
    }

    // 编辑轮播图操作
    public function edit_slideshow()
    {
        $image_file = input('post.image_file');
        $image_file_url = input('post.image_file_url/s', null);
        if (is_null($image_file_url)) {
            return json(['code' => 1, 'msg' => '上次的图片路径丢失']);
        }
        $img_path = '';
        $del_img_status = false;
        if (is_null($image_file)) {
            $image_files = request()->file('image_file');
            if (!$image_files) {
                return json(['code' => 2, 'msg' => '未接收到图片']);
            } else {
                $img_path = \think\facade\Filesystem::disk('public')->putFile('slideshow', $image_files);
            }
            $del_img_status = true;
        } else {
            $img_path = $image_file_url;
        }

        $id = input('post.id/d', null);
        $title = input('post.title/s', null);
        $nid = input('post.notice_id/d', null);
        $status = input('post.status/s', 1);

        $data['title'] = $title;
        $data['img_path'] = $img_path;
        $data['nid'] = $nid == 0 ? null : $nid;
        $data['status'] = $status;
        $data['update_time'] = time();

        // 加载轮播图的工具类
        $slideshow = new ModelSlideshow();
        $slideshow_res = $slideshow->set_slideshow_info_by_id_and_data($id, $data);

        if (!$slideshow_res) {
            try {
                unlink(app()->getRootPath() . 'public/storage/' . $img_path);
            } catch (\Throwable $th) {
            }
            return json(['code' => 2, 'msg' => '更新失败，请检查文章链接，以及文件大小 20M']);
        }

        if ($del_img_status) {
            try {
                unlink(app()->getRootPath() . 'public/storage/' . $image_file_url);
            } catch (\Throwable $th) {
            }
        }

        return json(['code' => 0, 'msg' => '更新成功']);
    }

    // 删除轮播图
    public function del_slideshow()
    {
        $id = input('post.id/d', null);
        if (is_null($id)) {
            return json(['code' => 1, 'msg' => 'ID 丢失']);
        }
        // 加载轮播图的工具类
        $slideshow = new ModelSlideshow();
        $slideshow_res = $slideshow->del_slideshow_info_by_id($id);
        if (!$slideshow_res) {
            return json(['code' => 2, 'msg' => '删除失败']);
        }
        return json(['code' => 0, 'msg' => '删除成功']);
    }
}
